﻿using _1._6._2015_EF_Pair.Data;
using _1._6._2015_EF_Pair.Data.Model;
using _1._6._2015_EF_Pair.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.Entity.Migrations;

namespace _1._6._2015_EF_Pair.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {

            PersonsDBContext db = new PersonsDBContext();
            PersonsViewModel model = new PersonsViewModel();

            model.Persons = db.Persons.Select(p => new PersonViewModel
                {
                     FirstName = p.FirstName,
                     LastName = p.LastName
                  
                }).ToList();

            return View(model);
        }

        //[HttpGet]
        //public ActionResult UpdatePerson(PersonViewModel model)
        //{
            

        //    return View(model);
        //}

         public ActionResult UpdatePerson(PersonViewModel model)
        {
            PersonsDBContext db = new PersonsDBContext();

            db.Persons.AddOrUpdate(p => new { p.FirstName, p.LastName },
                new Person
                {
                    FirstName = model.FirstName,
                    LastName = model.LastName
                }
                );

            return View(model);
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}